﻿package org.fas.ui
{
	import flash.events.MouseEvent;
	
	import org.fas.utils.FuGeom;
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class FButton extends FLabel  {
		public var colorFillUp:uint = 0xFF9900;
		public var colorFillDown:uint = 0xFFB452;
		public var colorFillOver:uint = 0xFFFF00;
		public var colorFillDisabled:uint = 0x999999;
		public var colorBorderUp:uint = 0x999999;
		public var colorBorderDown:uint = 0xEEEEEE;
		public var colorBorderOver:uint = 0x111111;
		public var colorBorderDisabled:uint = 0xaaaa00;
		public var alphaFill:Number = 0.8;
		public var alphaBorder:Number = 0.9;
		public var colorTextUp:uint = 0x000000;
		public var colorTextDown:uint = 0x000000;
		public var colorTextOver:uint = 0xFFFFFF;
		public var colorTextDisabled:uint = 0x666666;
		private var clickListener:Function;
		public function FButton(_label:String="FButton",_x:Number=0,_y:Number=0,_clickListener:Function=null) {
			super(_label,_x,_y);
			refer();
			clickListener = _clickListener;
		}
		public function refer():void{
			this.enabled = true;
			this.selected = false;
//			this.addChildAt(this.button,0);
		}
		private var enabled_r:Boolean;
		public function set enabled(_is:Boolean):void{
			this.buttonMode = _is;
			this.mouseEnabled = _is;
			if(_is){
				if(this.selected_r){
					this.color = this.colorTextDown;
					FuGeom.DrawRect(this,0,0,width,height,colorFillDown,alphaFill,colorBorderDown,alphaBorder);
				}else{
					this.color = this.colorTextUp;
					FuGeom.DrawRect(this,0,0,width,height,colorFillUp,alphaFill,colorBorderUp,alphaBorder);
				}
				this.addEventListener(MouseEvent.ROLL_OVER,this.onMover);
				this.addEventListener(MouseEvent.ROLL_OUT,this.onMout);
				this.addEventListener(MouseEvent.MOUSE_DOWN,this.onMdown);
				this.addEventListener(MouseEvent.MOUSE_UP,this.onMup);
			}else{
				this.color = this.colorTextDisabled;
				FuGeom.DrawRect(this,0,0,this.width,this.height,colorFillDisabled,alphaFill,colorBorderDisabled,alphaBorder);
				this.removeEventListener(MouseEvent.ROLL_OVER,this.onMover);
				this.removeEventListener(MouseEvent.ROLL_OUT,this.onMout);
				this.removeEventListener(MouseEvent.MOUSE_DOWN,this.onMdown);
				this.removeEventListener(MouseEvent.MOUSE_UP,this.onMup);
			}
			enabled_r = _is;
		}
		public function get enabled():Boolean{
			return enabled_r;
		}
		override public function set text(_text:String):void{
			super.text = _text;
			this.color = this.colorTextUp;
			FuGeom.DrawRect(this,0,0,this.width,this.height,colorFillUp,alphaFill,colorBorderUp,alphaBorder);
		}
		private var selected_r:Boolean;
		public function set selected(_is:Boolean):void{
			if(_is){
				this.color = this.colorTextDown;
				FuGeom.DrawRect(this,0,0,width,height,colorFillDown,alphaFill,colorBorderDown,alphaBorder);
			}else{
				this.color = this.colorTextUp;
				FuGeom.DrawRect(this,0,0,width,height,colorFillUp,alphaFill,colorBorderUp,alphaBorder);
			}
			if(!this.enabled){
				this.color = this.colorTextDisabled;
				FuGeom.DrawRect(this,0,0,this.width,this.height,colorFillDisabled,alphaFill,colorBorderDisabled,alphaBorder);
			}
			selected_r = _is;
		}
		public function get selected():Boolean{
			return selected_r;
		}
		private function onMover(_e:MouseEvent):void{
			this.color = this.colorTextOver;
			FuGeom.DrawRect(this,0,0,width,height,colorFillOver,alphaFill,colorBorderOver,alphaBorder);
		}
		private function onMout(_e:MouseEvent):void{
			if(this.selected_r){
				this.color = this.colorTextDown;
				FuGeom.DrawRect(this,0,0,width,height,colorFillDown,alphaFill,colorBorderDown,alphaBorder);
			}else{
				this.color = this.colorTextUp;
				FuGeom.DrawRect(this,0,0,width,height,colorFillUp,alphaFill,colorBorderUp,alphaBorder);
			}
		}
		private function onMdown(_e:MouseEvent):void{
			this.color = this.colorTextDown;
			FuGeom.DrawRect(this,0,0,width,height,colorFillDown,alphaFill,colorBorderDown,alphaBorder);
			if(this.clickListener!=null){
				this.clickListener(_e);
			}
		}
		private function onMup(_e:MouseEvent):void{
			this.color = this.colorTextOver;
			FuGeom.DrawRect(this,0,0,width,height,colorFillOver,alphaFill,colorBorderUp,alphaBorder);
		}
		override public function destruct():void{
			super.destruct();
			this.clickListener = null;
		}
	}
	
}